package logic

import (
	"context"
	"post/docker/mysql"
	"post/postrpc/internal/svc"
	"post/postrpc/postrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecordListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRecordListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecordListLogic {
	return &RecordListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RecordListLogic) RecordList(in *postrpc.RecordListRequest) (*postrpc.RecordListResponse, error) {
	// todo: add your logic here and delete this line
	var record []mysql.Record
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	mysql.DB.Debug().Offset(int(offset)).Limit(int(pageSize)).Order("create_at desc").Find(&record)
	var recordList []*postrpc.RecordList
	for _, m := range record {
		list := postrpc.RecordList{
			Id:         int64(m.ID),
			UserId:     m.UserId,
			Comment:    m.Comment,
			Title:      m.Title,
			ActivityId: m.ActivityId,
			ClassId:    m.ClassId,
		}
		recordList = append(recordList, &list)
	}

	return &postrpc.RecordListResponse{
		List: recordList,
	}, nil
}
